from queue import Queue
import threading
import re

class Dispatcher:
    handlers = []
    queues = []

    def reg(self,handle):
        q = Queue()
        self.queues.append(q)

        h = threading.Thread(target=handle,args=(q,))
        self.handlers.append(h)

    def run(self,data):
        for h in self.handlers:
            h.start()

        for item in data:
            print(item)
            for q in self.queues:  #生产者生成数据，打入多个队列中，一对多
                q.put(item)

        for q in self.queues:
            q.put(None)  #队列中最后加入None,避免handle线程死循环

def load_data(filename):
    with open(filename) as f:
        for line in f:
            line = line.strip()
            yield line

def handle_code(q:Queue):
    regex = re.compile('[0-9\.]+ - - \[[^\[\]]+\] "[^"]+" (?P<status>\d+) \d+ "http://[^"]+" "[^"]+"')
    count = 0
    while True:
        d = q.get()
        if d is not None:
            ret = regex.match(d)
            if ret:
                res = ret.group(1)
                count += 1
                print('状态码为：{} 出现的状态码总计{}次'.format(res,count))
        else:
            print('状态码任务处理完成')
            break

def handle_url(q:Queue):
    regex = re.compile('[0-9\.]+ - - \[[^\[\]]+\] "[^"]+" \d+ \d+ "(?P<url>http://[^"]+)" "[^"]+"')
    count = 0
    while True:
        d = q.get()
        if d is not None:
            ret = regex.match(d)
            if ret:
                res = ret.group(1)

                count += 1
                print('url为：{} 出现的url总计{}次'.format(res,count))
        else:
            print('url任务处理完成')
            break


d = Dispatcher()
d.reg(handle_code)
d.reg(handle_url)

d.run(load_data('access_2021_02_22.log'))

"""
可以将结果只显示汇总后的结果
"""


